package pl.edu.pk.cc.server.main;

import java.util.Vector;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import pl.edu.pk.cc.common.dto.CCAgreementDTO;
import pl.edu.pk.cc.common.dto.CCCallHistoryDTO;
import pl.edu.pk.cc.common.dto.CCClientDTO;
import pl.edu.pk.cc.common.dto.CCEmploymentDTO;
import pl.edu.pk.cc.common.dto.CCLoginDataDTO;
import pl.edu.pk.cc.common.dto.CCProjectsDTO;
import pl.edu.pk.cc.common.dto.CCSaleDTO;
import pl.edu.pk.cc.common.dto.CCScriptsDTO;
import pl.edu.pk.cc.common.dto.CCWorkerDTO;
import pl.edu.pk.cc.server.hibernate.CCHibernateUtil;

public class CCServletRequestExecutor {

	static Session session = CCHibernateUtil.getSessionFactory().getCurrentSession();

	public static Vector<Object> login(Vector<String> args) {

		Vector<Object> response = new Vector<>();
		CCLoginDataDTO loginData = checkLoginData(args);
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		CCWorkerDTO worker = (CCWorkerDTO) session.load(CCWorkerDTO.class, loginData.getId());
		response.add(CCServerSessionsManager.createSession(args.get(0), loginData));
		response.add(worker);
		session.close();
		return response;
	}

	public static boolean logout(Vector<Long> args) {
		CCServerSessionsManager.destroySession(args);
		return true;
	}

	protected static CCLoginDataDTO checkLoginData(Vector<String> args) {
		CCLoginDataDTO data = getLoginData(args.get(0));
		if (data != null && data.getPassword().equals(args.get(1))) {
			return data;
		}
		return null;
	}

	/**
	 * public static List<CCResourceDTO> getResourcesByID(String table, Vector<Long> params) { // TODO Auto-generated method stub return null; }
	 * 
	 * public static List<CCResourceDTO> getResourcesByString(String table, String column, Vector<String> params) { // TODO Auto-generated method stub return null; }
	 */
	protected static CCLoginDataDTO getLoginData(String login) {
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from CCLoginDataDTO where username=:user").setParameter("user", login);
		CCLoginDataDTO log = null;
		if (!q.list().isEmpty()) {
			log = (CCLoginDataDTO) q.list().get(0);
		}
		session.close();

		return log;
	}

	public static Vector<CCAgreementDTO> getAgreementByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCAgreementDTO> getAgreementByClientID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCCallHistoryDTO> getCallHistoryByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCCallHistoryDTO> getCallHistoryByClientID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCCallHistoryDTO> getCallHistoryByWorkerID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCClientDTO> getClientByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCClientDTO> getClientByName(Vector<String> params) {
		return null;// TODO
	}

	public static Vector<CCClientDTO> getClientByAddress(Vector<String> params) {
		return null;// TODO
	}

	public static Vector<CCEmploymentDTO> getEmploymentByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCEmploymentDTO> getEmploymentByPlacement(Vector<String> params) {
		return null;// TODO
	}

	public static Vector<CCEmploymentDTO> getEmploymentByWorkerID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCSaleDTO> getSaleByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCSaleDTO> getSaleByClientID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCWorkerDTO> getWorkerByID(Vector<Long> params) {
		return null;// TODO
	}

	public static Vector<CCWorkerDTO> getWorkerByName(Vector<String> params) {
		return null;// TODO
	}

	public static Vector<CCWorkerDTO> getWorkerByLogin(Vector<String> params) {
		return null;// TODO
	}

	public static void saveWorker(Vector<CCWorkerDTO> params) {
		// TODO
	}

	public static boolean saveSale(Vector<CCSaleDTO> params) {
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		for (CCSaleDTO call : params) {
			session.save(call);

		}
		session.flush();
		session.getTransaction().commit();
		return true;
	}

	public static void saveEmployment(Vector<CCEmploymentDTO> params) {
		// TODO
	}

	public static boolean saveCallHistory(Vector<CCCallHistoryDTO> params) {
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		for (CCCallHistoryDTO call : params) {
			session.save(call);

		}
		session.flush();
		session.getTransaction().commit();
		return true;

	}

	public static boolean saveAgreement(Vector<CCAgreementDTO> params) {
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		for (CCAgreementDTO call : params) {
			session.save(call);

		}
		session.flush();
		session.getTransaction().commit();
		return true;
	}

	public static void saveLoginData(Vector<CCLoginDataDTO> params) {
		// TODO
	}

	public static void saveClient(Vector<CCClientDTO> params) {
		// TODO
	}

	@SuppressWarnings("unchecked")
	public static Vector<CCClientDTO> getClientByWorkerID(Vector<Long> params) {
		Vector<CCClientDTO> clients = new Vector<>();
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		for (Long id : params) {
			Query q = session.createQuery("from CCClientDTO where username=:user").setParameter("user", id);
			if (!q.list().isEmpty()) {
				clients.addAll(q.list());
			}

		}

		session.close();
		return clients;
	}

	@SuppressWarnings("unchecked")
	public static Vector<CCAgreementDTO> getAgreementAll() {
		Vector<CCAgreementDTO> agreements = new Vector<>();
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from CCAgreementDTO");
		if (!q.list().isEmpty()) {
			Hibernate.initialize(q.list());
			agreements.addAll(q.list());
		}
		session.close();
		return agreements;
	}

	@SuppressWarnings("unchecked")
	public static Vector<CCProjectsDTO> getProjectsAll() {
		Vector<CCProjectsDTO> projects = new Vector<>();
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from CCProjectsDTO");
		if (!q.list().isEmpty()) {
			Hibernate.initialize(q.list());
			projects.addAll(q.list());
		}
		session.close();
		return projects;
	}

	@SuppressWarnings("unchecked")
	public static Vector<CCClientDTO> getClientsRand() {
		Vector<CCClientDTO> clients = new Vector<>();
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from CCClientDTO");
		if (!q.list().isEmpty()) {
			Hibernate.initialize(q.list());
			clients.addAll(q.list());
		}
		session.close();
		return clients;
	}

	@SuppressWarnings("unchecked")
	public static Vector<String> getScriptByType(Vector<String> params) {
		Vector<CCScriptsDTO> scripts = new Vector<>(0);
		session = CCHibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from CCScriptsDTO where type=:typ").setParameter("typ", params.get(0));
		if (!q.list().isEmpty()) {
			Hibernate.initialize(q.list());
			scripts.addAll(q.list());
		}
		session.close();
		Vector<String> selectedScript = new Vector<>();
		if (scripts.size() != 0) {
			selectedScript.add(scripts.get(0).getScript());
		} else {
			selectedScript.add("No Script!");
		}
		return selectedScript;
	}

}
